package com.example.demo;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.demo.util.CloseDefaultStyleUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends CloseDefaultStyleUtil implements View.OnClickListener {
    private ViewPager viewPager;
    private LinearLayout liFlowChart,liClock,liHistogram,LiSetup;
    private ImageView imFlowChart,imClock,imHistogram,imSetup;
    private List<Fragment> fragmentList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();  //初始化数据
        initView();  //初始化视图
    }

    private void initView() {
        //初始化页卡部分
        viewPager=findViewById(R.id.vp);
        //初始化底部导航栏所有控件
        liFlowChart =  findViewById(R.id.li_flow);
        liClock     =  findViewById(R.id.li_clock);
        liHistogram =  findViewById(R.id.li_histogram);
        LiSetup     =  findViewById(R.id.li_setup);
        imFlowChart =  findViewById(R.id.imageView_flow);
        imClock     =  findViewById(R.id.imageView_clock);
        imHistogram =  findViewById(R.id.imageView_histogram);
        imSetup     =  findViewById(R.id.imageView_setup);

        //设置监听
        liFlowChart.setOnClickListener(this);
        liClock.setOnClickListener(this);
        liHistogram.setOnClickListener(this);
        LiSetup.setOnClickListener(this);


        //加载页卡管理器
        FragmentManager fragmentManager=getSupportFragmentManager();
        viewPager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);//得到位置时要具体的数据，
            }
            @Override
            public int getCount() {
                return  fragmentList.size();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                setImage(position);
            }
            @Override
            public void onPageSelected(int position) {

            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        setImage(0);//用来初始化
    }
    private void setImage(int i) {
        resetImage();
        switch (i){
            case 0:{
                imFlowChart.setImageResource(R.mipmap.flow_chart_black);
                break;
            }
            case 1:{
                imClock.setImageResource(R.mipmap.clock_black);
                break;
            }
            case 2:{
                imHistogram.setImageResource(R.mipmap.histogram_black);
                break;
            }
            case 3:{
                imSetup.setImageResource(R.mipmap.setup_black);
                break;
            }

        }
    }
    private void resetImage() {
        imFlowChart.setImageResource(R.mipmap.flow_chart_gray);
        imClock.setImageResource(R.mipmap.clock_gray);
        imHistogram.setImageResource(R.mipmap.histogram_gray);
        imSetup.setImageResource(R.mipmap.setup_gray);
    }
    /**
     * 初始化数据
     */
    private void initData() {
        FlowChart flowChart=new FlowChart();
        Clock clock= new Clock();
        Histogram histogram=new Histogram();
        Setup setup=new Setup();
        fragmentList.add(flowChart);
        fragmentList.add(clock);
        fragmentList.add(histogram);
        fragmentList.add(setup);
    }


    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.li_flow:{
                setImage(0);
                viewPager.setCurrentItem(0);
            }
            break;
            case R.id.li_clock:{
                setImage(1);
                viewPager.setCurrentItem(1);
            }
            break;
            case R.id.li_histogram:{
                setImage(2);
                viewPager.setCurrentItem(2);
            }
            break;
            case R.id.li_setup:{
                setImage(3);
                viewPager.setCurrentItem(3);
            }
            break;
        }
    }
}